package com.web.webstart.base.service.impl;

import com.web.webstart.base.util.XaUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import java.util.List;

/**
 * @Title: BaseService.java
 * @Package com.web.xxx.business.refund_service.impl
 * @Description: 学习参见 http://blog.csdn.net/u010837612/article/details/47666131
 * 防止sql注入 {@link org.apache.commons.lang.StringEscapeUtils} escapeSql http://blog.csdn.net/chenleixing/article/details/43456987
 *
 * 注意：这里所有的操作都提交了
 * @author eason.zt
 * @date 2014年8月20日 下午6:55:24
 * @version V1.0
 */
public abstract class BaseService<T> {

	@Autowired
	private EntityManagerFactory entityManagerFactory;
	
	@SuppressWarnings("unchecked")
	public List<T> query(String sql,Class resultSetMapping){
		EntityManager em = entityManagerFactory.createEntityManager();
		Query query= em.createNativeQuery(sql,resultSetMapping);
		List<T> list = query.getResultList();
		em.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> query(String sql,Integer begin,Integer count){
		EntityManager em = entityManagerFactory.createEntityManager();
		Query query= em.createNativeQuery(sql);
		if(begin != null && count != null){
			query.setFirstResult(begin);
			query.setMaxResults(count);
		}
		List<Object[]> list = query.getResultList();
		em.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> queryString(String sql,Integer begin,Integer count){
		EntityManager em = entityManagerFactory.createEntityManager();
		Query query= em.createNativeQuery(sql);
		if(begin != null && count != null){
			query.setFirstResult(begin);
			query.setMaxResults(count);
		}
		List<String> list = query.getResultList();
		em.close();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> queryParams(String sql,Integer begin,Integer count,List<? extends Object> params){
		EntityManager em = entityManagerFactory.createEntityManager();
		Query query= em.createNativeQuery(sql);
		if(XaUtil.isNotEmpty(params)){
			for(int i=0;i<params.size();i++){
				query.setParameter(i+1, params.get(i));
			}
		}
		if(begin != null && count != null){
			query.setFirstResult(begin);
			query.setMaxResults(count);
		}
		List<Object[]> list = query.getResultList();
		em.close();
		return list;
	}


	@SuppressWarnings("unchecked")
    //可以调用基本的sql,存储过程等
	public List<Object[]> queryCall(String procString,List<? extends Object> params){
		EntityManager em = entityManagerFactory.createEntityManager();
		Query query= em.createNativeQuery(procString);
		if(XaUtil.isNotEmpty(params)){
			for(int i=0;i<params.size();i++){
				query.setParameter(i+1, params.get(i));
			}
		}
		List<Object[]> list = query.getResultList();
		em.close();
	    return list;
	}

	//增删改
	public Integer update(String sql){
		EntityManager em = entityManagerFactory.createEntityManager();
		em.getTransaction().begin();
		Query query= em.createNativeQuery(sql);
		Integer r = query.executeUpdate();
		em.getTransaction().commit();
		em.close();
		return r;
	}

    //增删改
	public Integer update(String sql,List<? extends Object> params){
		EntityManager em = entityManagerFactory.createEntityManager();
		em.getTransaction().begin();
		Query query= em.createNativeQuery(sql);
		if(XaUtil.isNotEmpty(params)){
			for(int i=0;i<params.size();i++){
				query.setParameter(i+1, params.get(i));
			}
		}
		Integer r = query.executeUpdate();
		em.getTransaction().commit();
		em.close();
		return r;
	}

	//增删改(注意使用hsql语句)
	public Integer hsqlUpdate(String hsql,List<? extends Object> params){
		EntityManager em = entityManagerFactory.createEntityManager();
		em.getTransaction().begin();
		Query query= em.createQuery(hsql);
		if(XaUtil.isNotEmpty(params)){
			for(int i=0;i<params.size();i++){
				query.setParameter(i+1, params.get(i));
			}
		}
		Integer r = query.executeUpdate();
		em.getTransaction().commit();
		em.close();
		return r;
	}
}

